package www.bancodelanacion.intranet.prestamos.controlador;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import www.bancodelanacion.intranet.prestamos.entidad.ClienteDTO;
import www.bancodelanacion.intranet.prestamos.service.ClienteService;
import www.bancodelanacion.intranet.prestamos.service.ClienteServiceImpl;

/**
 * Servlet implementation class servletCliente
 */
public class servletCliente extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String metodo = request.getParameter("metodo");
		if (metodo.equals("inserta")) {
			inserta(request, response);
			lista(request, response);
		} else if (metodo.equals("elimina")) {
			elimina(request, response);
			lista(request, response);
		} else if (metodo.equals("actualiza")) {
			actualiza(request, response);
			lista(request, response);
		} else if (metodo.equals("lista")) {
			lista(request, response);
		} else if (metodo.equals("busca")) {
			busca(request, response);
		}
	}
	ClienteService servicio = new ClienteServiceImpl();
	protected void inserta(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			ClienteDTO p = new  ClienteDTO();
			
			
			servicio.insertaCliente(p);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}

	protected void elimina(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			int codigo = Integer.parseInt(request.getParameter("codigo"));
			servicio.eliminaCliente(codigo);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	protected void actualiza(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			ClienteDTO p = new  ClienteDTO();
		
			
			servicio.actualizaCliente(p);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}

	protected void lista(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		ArrayList<ClienteDTO> lista1 = null;
		try {
			lista1 = servicio.listarCliente();
			System.out.println("termino lista");
		} catch (SQLException e) {
			// TODO: handle exception
		}
		
		//para que pueda paginar se gurada en una sesion la lista
		request.getSession().setAttribute("tabla", lista1);
		request.getRequestDispatcher("/MantenerCliente.jsp").forward(request,
				response);
		System.out.println("mando lista");
	}

	protected void busca(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			int codigo = Integer.parseInt(request.getParameter("codigo"));
			ClienteDTO bean = servicio.buscaCliente(codigo);
			request.setAttribute("producto", bean);
			request.getRequestDispatcher("/actualizaCliente.jsp").forward(request,response);        
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	}


